<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')"
                      >添加子部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形结构部门列表 -->
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdit(data.id)"
                          >编辑子部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="doDelete(data.id)"
                          >删除子部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 模态弹框 -->

      <el-dialog
        :title="this.isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="clerClose"
      >
        <AddorEdit
          :cru-id="cruId"
          @success="hsuccess"
          :is-edit="isEdit"
          ref="deptDialog"
          @close="hClose"
          :origin-list="originList"
        >
        </AddorEdit>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartment, DeleteDepartment } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
import AddorEdit from "./deptDialog";
export default {
  data() {
    return {
      list: [],
      // 弹框隐藏显示
      showDialog: false,
      cruId: "",
      isEdit: false, //false 添加 true 编辑
      originList: [],
    };
  },
  created() {
    this.setDepartment();
  },
  methods: {
    //弹框发生变化时触发
    clerClose() {
      this.$refs.deptDialog.resetForm();
    },
    //添加部门
    hAdd(id) {
      this.isEdit = false;
      this.showDialog = true;
      this.cruId = id;
    },
    //编辑部门
    hEdit(id) {
      this.isEdit = true;
      this.showDialog = true;
      this.cruId = id;
      this.$nextTick(() => {
        this.$refs.deptDialog.loadDetail();
      });
    },
    // 删除部门
    doDelete(id) {
      this.$confirm("确定要删除此部门吗？, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await DeleteDepartment(id);

          this.setDepartment();
        })
        .catch(() => {});
    },
    //关闭模态框，刷新列表
    hsuccess() {
      this.showDialog = false;
      this.setDepartment();
    },
    //发送部门列表请求
    async setDepartment() {
      try {
        const {
          data: { depts: res },
        } = await getDepartment();
        this.originList = res.map(({ id, pid, code, name }) => {
          return { id, pid, code, name };
        });
        res.shift();
        this.list = tranListToTreeData(res);
      } catch (error) {}
    },
    //子组件点击取消关闭弹层
    hClose() {
      this.showDialog = false;
      this.$refs.deptForm.resetFields();
    },
  },
  components: {
    AddorEdit,
  },
};
</script>